/*
Copyright 2011 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.google.rsm.core;

/**
 * An implementation of {@code Event} that implements {@link
 * Event#getType} with class equality semantics, guaranteeing that all
 * instances of this class will have the same type in the state
 * machine library.
 *
 * @author arunabhaghosh@google.com (Arunabha Ghosh)
 */
public class ClassEvent implements Event {
  private final EventType type;

  /**
   * Create a new instance of ClassEvent with the given type.
   *
   * <p>The intention is for derived classes to call {@code super}
   * as follows:
   *
   * <pre>
   * class SomeEvent extends ClassEvent {
   *   public static final EventType TYPE = new EventTypes.create();
   *
   *   public SomeEvent() {
   *     super(TYPE);
   *   }
   * }
   * </pre>
   *
   * This ensures that every derived type has a unique EventType.
   * It also allows other code to reference the type without creating
   * an instance of {@code SomeEvent}, like this:
   *
   * <pre>
   * EventType type = SomeEvent.TYPE;
   * </pre>
   *
   * @param type the event type to return from {@link #getType}.
   */
  public ClassEvent(EventType type) {
    this.type = type;
  }

  /**
   * {@inheritDoc}
   *
   * Returns the type supplied to the constructor.
   */
  @Override
  public EventType getType() {
    return type;
  }

  /**
   * Return the human readable version representation of this event.
   */
  @Override
  public String toString() {
    return type.toString();
  }
}
